import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog,List } 	from 'vant';
Vue.use(Toast,Dialog,List);
import {BigNumber}  from 'bignumber.js'

export default {
	name: 'redExchange',
	data(){
		return {
			psd:'',			//口令
			clickBtn:false,	//红包是否领取中
			whatapp_no:'',
			hiddenWhatsApp:false,		//是否隐藏whatsApp
		}
	},
	methods:{
		share(type){
			util.callHandler('showShareInfo',{
				type,
				phone:this.whatapp_no,
				shareData: {
        			title: '',
        			content: '',
        			url: '',
      			}
			},res => {
				console.log(res)
			})
		},
		psdBlur(){
			setTimeout(()=>{
				window.scrollTo(0, 0);
			},100)
		},
		send(){
			util.callHandler('openUrl',{
				url:'bbjApp://createRedPacket',
				title: this.$t('redPackage6'),
				needLogin:true,
				popVC:false,		//跳转的时候是否干掉当前页面
				hiddenNav:true		//是否全屏
			},res => {
				console.log(res)
			})
		},
		open(){
			//打开红包
			if(this.clickBtn) return
			if(!this.psd){
				Toast(this.$t('redPackage4'));
				return
			}
			this.clickBtn = true;
			util.fetch(`${util.ajaxUrl}/red-packet/get-packet`,{
				red_packet_pwd:this.psd,
				type:2
			}).then(res => {
				this.psd = '';
				this.clickBtn = false;
				if(res.retCode === 0){
			        const getParams = {
		            	nickname: res.retData.name,
		            	url: res.retData.url,
		            	btc: res.retData.user_amount,
		            	coinType: res.retData.currency_id,
		            };
		            this.$router.push({
						path:'/redetails',
						query:{
							rid:res.retData.rid,
							type:2,
							info:encodeURIComponent(JSON.stringify(getParams)),
						}
					})
				}else{
					Toast(res.retMsg || res.retHtml || this.$t('noError'))
				}
			})
		},
		init(){
			if(this.isNi){
				let params = {
                    type:1
                }
                let md5Key = util.getHashStr(params)
                params['sign'] = md5Key
				util.fetch(`${util.ajaxUrl}/nigeria/user/whatsapp`,params).then(res => {
					if(res.retCode === 0){
				        this.whatapp_no = res.retData.whatsapp_no;
					}else{
						Toast(res.retMsg || res.retHtml || this.$t('noError'))
					}
				})
			}
		},
		checkOut(){
			//尼日利亚版本判断是否领过红包
			util.fetch2(`${util.ajaxUrl}/red-packet/user-packet-list`,{currency_id:'USDT'}).then(res => {
				Toast.clear();
				if(res.retCode === 0){
					if(res.retData.total > 0){
						this.hiddenWhatsApp = true;
					}
					window.localStorage.setItem('hiddenWhatsApp',res.retData.total > 0?2:1)
				}else{
					this.finished = true;
					Toast(res.retMsg || res.retHtml || this.$t('noError'))
				}
			}).catch(error => {
				this.finished = true;
				console.log(error)
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		util.waitSDKReady().then(token => {
			if(this.isNi){
				if(window.localStorage.getItem('hiddenWhatsApp') && window.localStorage.getItem('hiddenWhatsApp')==='2'){
					this.hiddenWhatsApp = true;
				}else{
					this.hiddenWhatsApp = false;
				}
				this.checkOut();
			}
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'light'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				window.token = '';
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'light'
			})
		},500)
    }
}